<?php
	
	/*
	 * All the lines with # are other ways of doing it
	 */
	
	class Upload_files extends Controller
	{
		#function __construct() // PHP5 only
		function Upload_files()
		{
			parent::Controller();
			
			$this->load->library('form_validation');
			$this->load->helper(array('form','url'));
		}
		
		function index()
		{
			/* use views in your application this is 
			 * just a simple controller only example
			 */
			
			$this->view_header();
			$this->view_form_single_file();
			$this->view_form_multiple_files();
			$this->view_form_array_files();
			$this->view_form_array_files_jquery();
			$this->view_footer();
		}
		
		function upload_single_file()
		{
			$config['upload_path']   = './upload/';
			$config['allowed_types'] = 'gif|jpg';
			$config['encrypt_name']  = true;
			// or file specific
			#$config['single_file']['upload_path'] 	 = './uploads/';
			#$config['single_file']['allowed_types'] = 'gif|jpg';
			#$config['single_file']['encrypt_name']  = true;
			
			$this->load->library('upload',$config); // extended uploadclass
			
			#if (!$this->upload->do_upload()) // default input name userfile
			if (!$this->upload->do_upload('single_file')) // input name (type file)
			{
				// normal way
				#$error = array('error' => $this->upload->display_errors());
				#$this->load->view('upload_form',$error);
				
				$this->view_form_single_file($this->upload->display_errors());
			}	
			else
			{
				// normal way
				#$data = array('upload_data' => $this->upload->data()); // default all
				#$data = array('upload_data' => $this->upload->data('single_file'));
				#$this->load->view('upload_success',$data);
				
				echo '<pre>'; print_r($this->upload->data()); echo '</pre>';
				#echo '<pre>'; print_r($this->upload->data('single_file')); echo '</pre>';
			}
		}
		
		function upload_multiple_files()
		{
			// array name 'file_1' must be the same as input name
			#$config['file_1']['upload_path'] 	= './uploads/';
			#$config['file_1']['allowed_types']  = 'gif|jpg';
			#$config['file_1']['encrypt_name'] 	= true;
			#$config['file_2']['upload_path'] 	= './uploads/';
			#$config['file_2']['allowed_types']  = 'gif|jpg';
			#$config['file_2']['encrypt_name'] 	= true;
			#$config['file_3']['upload_path'] 	= './uploads/';
			#$config['file_3']['allowed_types']  = 'gif|jpg';
			#$config['file_3']['encrypt_name'] 	= true;
			// or config for all files the same
			$config['upload_path']   = './upload/';
			$config['allowed_types'] = 'gif|jpg';
			$config['encrypt_name']  = true;
			
			$this->load->library('upload',$config);
			
			#if (!$this->upload->do_upload()) // default input name userfile[]
			#if (!$this->upload->do_upload('file_1,file_2,file_3')) // csv of input names (type file)
			if (!$this->upload->do_upload(array('file_1','file_2','file_3'))) // array of input names (type file)
			{
				// normal way
				#$error = array('error' => $this->upload->display_errors());
				#$this->load->view('upload_form',$error);
				
				$this->view_form_multiple_files($this->upload->display_errors());
			}	
			else
			{
				// normal way
				#$data = array('upload_data' => $this->upload->data()); // default all
				#$data = array('upload_data' => $this->upload->data('file_1,file_2,file_3'));
				#$data = array('upload_data' => $this->upload->data(array('file_1','file_2','file_3')));
				#$this->load->view('upload_success',$data);
				
				echo '<pre>'; print_r($this->upload->data()); echo '</pre>'; // default all
				#echo '<pre>'; print_r($this->upload->data('file_1,file_2,file_3')); echo '</pre>';
				#echo '<pre>'; print_r($this->upload->data(array('file_1','file_2','file_3'))); echo '</pre>';
			}
		}
		
		function upload_array_files() // almost same as upload_single_file
		{
			// config for all files in array the same
			$config['upload_path'] 	 = './upload/';
			$config['allowed_types'] = 'gif|jpg';
			$config['encrypt_name']  = true;
			
			$this->load->library('upload',$config);
			
			#if (!$this->upload->do_upload()) // default input name userfile[]
			if (!$this->upload->do_upload('files')) // input array name (type file) without []
			{
				// normal way
				#$error = array('error' => $this->upload->display_errors());
				#$this->load->view('upload_form',$error);
				
				$this->view_form_array_files($this->upload->display_errors());
			}	
			else
			{
				// normal way
				#$data = array('upload_data' => $this->upload->data()); // default all
				#$data = array('upload_data' => $this->upload->data('files'));
				#$this->load->view('upload_success',$data);
				
				echo '<pre>'; print_r($this->upload->data()); echo '</pre>'; // default all
				#echo '<pre>'; print_r($this->upload->data('files')); echo '</pre>';
			}
		}
		
		function upload_array_files_jquery() // same as upload_array_files
		{
			// config for all files in array the same
			$config['upload_path'] 	 = base_url().'./uploads/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['encrypt_name']  = true;
			
			$this->load->library('upload',$config);
			
			#if (!$this->upload->do_upload()) // default input name userfile[]
			if (!$this->upload->do_upload('files')) // input array name (type file) without []
			{
				// normal way
				#$error = array('error' => $this->upload->display_errors());
				#$this->load->view('upload_form',$error);
				
				$this->view_form_array_files_jquery($this->upload->display_errors());
			}	
			else
			{
				// normal way
				#$data = array('upload_data' => $this->upload->data()); // default input name userfile[]
				#$data = array('upload_data' => $this->upload->data('files'));
				#$this->load->view('upload_success',$data);
				
				#echo '<pre>'; print_r($this->upload->data()); echo '</pre>'; // default input name userfile[]
				echo '<pre>'; print_r($this->upload->data('files')); echo '</pre>';
			}
		}
		
		function view_header()
		{
			// javascript only needed for jQuery example
			
			echo '<html><head>';
			echo '<script src="http://www.google.com/jsapi"></script>';
			echo '<script type="text/javascript">google.load("jquery","1");</script>';
			echo '<script src="http://jquery-multifile-plugin.googlecode.com/svn/trunk/jquery.MultiFile.pack.js" type="text/javascript"></script>';
			echo '<script type="text/javascript">';
			echo 'function OnLoad(){ $("jqform").MultiFile({list:"#filelist"}); };';
			echo 'google.setOnLoadCallback(OnLoad);';
			echo '</script>';
			echo '</head><body>';
		}
		
		function view_form_single_file($errors='')
		{
			echo '<h2>Single file</h2>';
			echo form_open_multipart('upload_files/upload_single_file');
			echo '<p>'.form_upload('single_file').'</p>';
			echo '<p>'.form_submit('','Upload').'</p>';
			echo form_close();
			
			if ($errors) echo '<p>'.$errors.'</p>';
		}
		
		function view_form_multiple_files($errors='')
		{
			echo '<h2>Multiple files</h2>';
			echo form_open_multipart('upload_files/upload_multiple_files');
			echo '<p>'.form_upload('file_1').'</p>';
			echo '<p>'.form_upload('file_2').'</p>';
			echo '<p>'.form_upload('file_3').'</p>';
			echo '<p>'.form_submit('','Upload').'</p>';
			echo form_close();
			
			if ($errors) echo '<p>'.$errors.'</p>';
		}
		
		function view_form_array_files($errors='')
		{
			echo '<h2>Array of files</h2>';
			echo form_open_multipart('upload_files/upload_array_files');
			echo '<p>'.form_upload('files[]').'</p>';
			echo '<p>'.form_upload('files[]').'</p>';
			echo '<p>'.form_upload('files[]').'</p>';
			echo '<p>'.form_submit('','Upload').'</p>';
			echo form_close();
			
			if ($errors) echo '<p>'.$errors.'</p>';
		}
		
		function view_form_array_files_jquery($errors='')
		{
			echo '<h2>Array of files - jQuery Multi File Upload Plugin</h2>';
			echo form_open_multipart('upload_files/upload_array_files',array('id'=>'jqform'));
			echo '<p>'.form_upload('files[]',null,'class="multi" accept="gif|jpg" maxlength="3"').'</p>';
			echo '<p id="filelist"></p>';
			echo '<p>'.form_submit('','Upload').'</p>';
			echo form_close();
			
			if ($errors) echo '<p>'.$errors.'</p>';
			
			echo anchor('http://jquery.com','jQuery',array('target'=>'_blank')).' ';
			echo anchor('http://www.fyneworks.com/jquery/multiple-file-upload/','Multiple File Upload Plugin',array('target'=>'_blank')).' or try ';
			echo anchor('http://www.uploadify.com','Uploadify (flash) Plugin',array('target'=>'_blank'));
		}
		
		function view_footer()
		{
			echo '</body></html>';
		}
	}
?>
